package com.eCommerce.server.sisv.impl;

import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.eCommerce.corecontract.criteria.ComposantCriteria;
import com.eCommerce.corecontract.dto.CategorieDTO;
import com.eCommerce.corecontract.dto.ComposantDTO;
import com.eCommerce.corecontract.exception.EcommerceException;
import com.eCommerce.corecontract.exception.EcommercePersisenceException;
import com.eCommerce.corecontract.exception.EcommerceSISVException;
import com.eCommerce.server.corebackend.dao.DAOController;
import com.eCommerce.server.corebackend.dao.ICategorieDAO;

import com.eCommerce.server.corebackend.dao.IComposantDAO;
import com.eCommerce.server.corebackend.sisv.IComposantSISV;
import com.eCommerce.server.corebackend.util.BackEndConstants;

public class ComposantSISVImpl extends AbstractCommonSISV implements IComposantSISV{
	
	private static Logger log =Logger.getLogger(ComposantSISVImpl.class);
	private DAOController _daoController = null;

	private IComposantDAO _composantDAOImpl=null;
	private ICategorieDAO _categorieDAOImpl=null;
	
	public ComposantSISVImpl(DAOController daoController) throws Exception{
		_daoController = daoController;
		_composantDAOImpl = (IComposantDAO) _daoController.get(BackEndConstants.DAO_COMPOSANT);
		
	
		
	}

	public Long ajouterComposant(ComposantDTO composantDTO) {
		// TODO Auto-generated method stub
		try {
		
			 _composantDAOImpl.insert(composantDTO);
			 composantDTO=_composantDAOImpl.findOne(composantDTO);
			 composantDTO.setCode("PRODUIT/"+composantDTO.getId().toString());
			 _composantDAOImpl.update(composantDTO);
			 return composantDTO.getId();
			 
			
		} catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		}
		catch (Exception e) {
			Object[] arg = new Object[1];
			arg[0]=composantDTO.getLibelle();
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMPOSANT-001", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
		
	}

	public List listerComposant() throws EcommerceSISVException {
		// TODO Auto-generated method stub
		try {
			return _composantDAOImpl.findAll();
		} catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		}
		catch (Exception e) {
			Object[] arg = new Object[1];
			
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMPOSANT-004", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	
	}

	public void modifierComposant(ComposantDTO composantDTO)
			throws EcommerceSISVException {
		// TODO Auto-generated method stub
		try {
			_composantDAOImpl.update(composantDTO);
		} catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		}
		catch (Exception e) {
			Object[] arg = new Object[1];
			arg[0]=composantDTO.getLibelle();
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMPOSANT-002", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
		
	}

	public List rechercherComposant(ComposantCriteria composantCriteria)
			throws EcommerceSISVException {
		// TODO Auto-generated method stub
		try {
			return _composantDAOImpl.find(composantCriteria);
		} catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		}
		catch (Exception e) {
			Object[] arg = new Object[1];
			
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMPOSANT-004", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
		
	}

	public void supprimerComposant(ComposantDTO composantDTO)
			throws EcommerceSISVException {
		// TODO Auto-generated method stub
		try {
			_composantDAOImpl.delete(composantDTO);
		} catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		}
		catch (Exception e) {
			Object[] arg = new Object[1];
			arg[0]=composantDTO.getLibelle();
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMPOSANT-005", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
		
	}

	public boolean validerComposant(ComposantDTO composantDTO)
			throws EcommerceSISVException {
		// TODO Auto-generated method stub
		return false;
	}

	public ComposantDTO visualiserComposant(ComposantDTO composantDTO)
			throws EcommerceSISVException {
		// TODO Auto-generated method stub
		try {
			return _composantDAOImpl.findOne(composantDTO);
		} catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		}
		catch (Exception e) {
			Object[] arg = new Object[1];
			
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMPOSANT-003", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
		
	}

	public List rechercherComposantParCategorie(
			CategorieDTO categorieDTO) throws EcommerceException {
		try {
			return _composantDAOImpl.findComposantByIdCategorie(categorieDTO);
		} catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		}
		catch (Exception e) {
			Object[] arg = new Object[1];
			
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCOMPOSANT-004", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	
	}
	

}
